﻿function addtabs(title, href) {
	var isexists = $('#body_tabs').tabs('exists', title);
	if (isexists) {
		var isexists = $('#body_tabs').tabs('select', title);
	} else {
		$('#body_tabs').tabs('add', {
			title : title,
			content : "<iframe src=\"" + href
					+ "\"scrolling=\"auto\" width=\"100%\" height=\"100%\" frameborder=\"0\" style=\"padding: 0 0 0 0 ;margin:0 0 0 0; \"></iframe>",
			closable : true
		});
	}

}
function addtab(title, href) {
	$('#body_tabs').tabs('add', {
		title : title,
		content : "<iframe src=\"" + href
				+ "\"scrolling=\"auto\" width=\"100%\" height=\"100%\" frameborder=\"0\" style=\"padding: 0 0 0 0 ;margin:0 0 0 0; \"></iframe>",
		closable : true
	});
}
/**
 * 关闭当前选中的tab
 */
function closeCurrentTab() {
	var tab = $('#body_tabs').tabs('getSelected');
	var index = $('#body_tabs').tabs('getTabIndex', tab);
	$("#body_tabs").tabs('close', index);
}

/**
 * 更新当前选中tab
 * 
 * @param tab_title
 * @param tab_href
 * @return
 */
function closeAndOpenTab(tab_title, tab_href) {
	closeCurrentTab();
	addtabs(tab_title, tab_href);
}
function logoff() {
	$.messager.confirm('提示', '你确定要退出系统吗?', function(r) {
		if (r) {
			location.href = 'logoff.do';
		}
	});
}

function profile() {
	$('#pdialog').dialog('open');
}

/**
 * 修改用户信息
 */
function editprofile() {
	if($("#UPwd").textbox('getValue')!=$("#cpwd").textbox('getValue')){
		alert("两次输入密码不一致！");
		return;
	}
	Common.Util.submitForm('pform', 'user/editInfo.do', function (msg) {
		$("#pform").form('reset');
		$('#pdialog').dialog('close');
	});
}

// 几个关闭事件的实现
function CloseTab(menu, type) {
	var curTabTitle = $(menu).data("tabTitle");
	var tabs = $("#body_tabs");

	if (type === "close") {
		if (curTabTitle !== "任务消息列表") {
			tabs.tabs("close", curTabTitle);
			return;
		} else {
			Common.Util.showMessager({msg:'该窗口不可以关闭'});
		}
	}
	var allTabs = tabs.tabs("tabs"); // 返回所有的 tab panel对象
	var closeTabsTitle = [];

	$.each(allTabs, function() {
		var opt = $(this).panel("options");
		if (opt.closable && opt.title != curTabTitle && type === "Other") {
			closeTabsTitle.push(opt.title);
		}
		if (opt.closable && type === "All") {
			closeTabsTitle.push(opt.title);
		}
	});

	for (var i = 0; i < closeTabsTitle.length; i++) {
		tabs.tabs("close", closeTabsTitle[i]);
	}
}

/* tabs鼠标右击事件,弹出菜单 */
function popupMenu (e, title, index) {
    e.preventDefault();
    $('#tabsMenu').menu('show', {
		left : e.pageX,
		top : e.pageY
	});
	$('#tabsMenu').data("tabTitle", title);
};

$(function(){
	// 实例化menu的onClick事件
	$("#tabsMenu").menu({
		onClick : function(item) {
			CloseTab(this, item.id);
		}
	});
});

/**
 * 菜单初始化工厂
 */
var MenuInitFaction = {
	'data' : [],
	'menuid' : 'menu_container_context',
	'style' : {
		'tree' : treeMenu,
		'accordion' : accordionMenu
	}
};

/**
 * 实例化菜单
 * 
 * @param {String}
 *            type 菜单风格
 */
function inital_menu(style) {
//	style = 'tree';
	$.ajax({
		async : true,
		type : 'post',
		dataType : 'json',
		url : 'main/navigation_tree.tree',
		success : function(data) {
			MenuInitFaction.data = data;
			if (style) {
				MenuInitFaction.style[style](data);
			} else {
				MenuInitFaction.style['accordion'](data);
			}
		}
	});
}

/**
 * 树风格菜单
 */
function treeMenu() {
	var tree = $('#' + MenuInitFaction.menuid).tree({
		data : MenuInitFaction.data,
		loadFilter : convert,
		onClick : function (node) {
			if (tree.tree('isLeaf', node.target)) {
				eval('(' + node.attributes.a_href + ')');
			}
		}
	});
}

/**
 * 风琴风格菜单
 */
function accordionMenu() {
	var rows = MenuInitFaction.data;
	var accordion = $('#' + MenuInitFaction.menuid).accordion({
		fit : true
	});
	for (var i = 0; i < rows.length; i++) {
		var obj = rows[i];
		if ((!obj.parentId) || (obj.parentId == -1)) {
			var content = "";
			for (var j = 0; j < rows.length; j++) {
				var obj_child = rows[j];
				if (obj_child.parentId == obj.id) {
					content += '<a  class="navigation" href="javascript:'
							+ obj_child.a_href + '"  >';
					content += '<img src="res/img/menu/' + obj_child.a_icon + '" title="'+obj_child.a_remarks+'" /><br>';
					content += obj_child.text + '</a><br><br>';
				}
			}
			
			accordion.accordion('add', {
				title : obj.text,
				selected : false,
				content : '<center>' + content + '</center>'
			});
		}
	}
	accordion.accordion('select', 0);
}